package com.zj.core.activity

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import com.dialogutils.ToastUtil
import com.moduleinterface.InterfaceFactory
import com.systemutils.PermissionHelper
import com.zj.core.R
import com.zj.core.activity.ConfigActivity.Companion.RES_CODE_CONFIG
import com.zj.core.interfaces.annotations.Permissions
import com.zj.core.base.BaseActivity
import com.zj.core.base.BaseApplication
import com.zj.core.ui.LoginActivityUi
import org.jetbrains.anko.*

class LoginActivity : BaseActivity(), View.OnClickListener, View.OnLongClickListener {
    private var ScanActivityRequestCode = 1     //扫码后返回
    private val REQ_CODE_CONFIG = 2

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LoginActivityUi(this, this).setContentView(this)

        if (!isTaskRoot && intent != null) {
            val action = intent.action
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN == action) {
                finish()
                return
            }
        }
        loadPermission()
    }

    @Permissions(value = [
        Manifest.permission.INTERNET,
        Manifest.permission.RECORD_AUDIO,
        Manifest.permission.ACCESS_NETWORK_STATE,
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE])
    private fun loadPermission() {

    }

    /**
     * 点击扫描按钮
     */
    @Permissions(value = [
        Manifest.permission.CAMERA,
        Manifest.permission.READ_PHONE_STATE])
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.login_scan_bu -> {
                InterfaceFactory.getInstance().getmInterfaceQR().scanQRcode(this, "登录", 2)
            }
        }
    }

    /**
     * 长按扫描按钮
     */
    override fun onLongClick(v: View?): Boolean {
        when (v?.id) {
            R.id.login_scan_bu -> {
                activityJumpForResult(this, REQ_CODE_CONFIG, ConfigActivity().javaClass, Bundle())
            }
        }
        return true
    }

    /**
     * 条码扫描返回
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        Log.d("barcodeStatus_1", "" + ScanActivityRequestCode)

        when (requestCode) {
            ScanActivityRequestCode -> {     // 条码识别
                Log.d("barcodeStatus_2", "" + data?.getStringExtra("barcode"))
                if (data?.getStringExtra("barcode") != null) {
//                    ToastUtil.showShort(BaseApplication.context,"识别条码信息为："+data.getStringExtra("barcode"))

                    with(Bundle()) {
                        this.putString("nurseId", data.getStringExtra("barcode"))
                        activityJump(this@LoginActivity, MainActivity().javaClass, this)
                        finish()
                        Log.d("barcodeStatus_3", "跳转结束")
                    }
                } else {
                    ToastUtil.showShort(BaseApplication.context, "未识别到条码信息")
                }
            }

            REQ_CODE_CONFIG -> {                      //配置界面返回
                if (resultCode == RES_CODE_CONFIG) {

                }
            }
        }
    }

    override fun onDestroy() {
        InterfaceFactory.getInstance().getmInterfaceQR().destroyQR()
        PermissionHelper.getInstance().destroyHelper()
        super.onDestroy()
    }
}
